import jdk.nashorn.internal.runtime.WithObject;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.IntStream;

/**
 * Created by zhourh on 2018/5/24.
 * 给定一个单词列表，只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。


 American keyboard


 示例1:

 输入: ["Hello", "Alaska", "Dad", "Peace"]
 输出: ["Alaska", "Dad"]
 注意:

 你可以重复使用键盘上同一字符。
 你可以假设输入的字符串将只包含字母。

 查表题
 */
public class KeywordRow {

    private static final Set<Character> row1 = new HashSet<>(Arrays.asList(new Character[]{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' }));
    private static final Set<Character> row2 = new HashSet<>(Arrays.asList(new Character[]{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' }));
    private static final Set<Character> row3 = new HashSet<>(Arrays.asList(new Character[]{'z', 'x', 'c', 'v', 'b', 'n', 'm' }));

    public static void main(String[] args) {
        System.out.println(Arrays.asList(new KeywordRow().findWords(new String[]{"Hello", "Alaska", "Dad", "Peace"})));
    }

    public String[] findWords(String[] words) {
        if (words == null) {
            return null;
        }
        List<String> oneRowWords = new ArrayList<>();

        for (String word : words) {
            String lowerCaseWord = word.toLowerCase();
            if (containsAll(row1, lowerCaseWord) || containsAll(row2, lowerCaseWord) || containsAll(row3, lowerCaseWord)) {
                oneRowWords.add(word);
            }
        }

        return oneRowWords.toArray(new String[oneRowWords.size()]);
    }

    private boolean containsAll(Set<Character> set, String word) {
        if (set == null) {
            return false;
        }

        if (word == null || word.isEmpty()) {
            return true;
        }

        for (char c : word.toCharArray()) {
            if (set.contains(c)) {
                continue;
            } else {
                return false;
            }
        }

        return true;
    }

}
